package com.jshoperxms.controller.mall.backstage.community;

import com.jshoperxms.Response;
import com.jshoperxms.controller.mall.backstage.base.BaseTController;
import com.jshoperxms.controller.mall.backstage.base.IBaseCtrl;
import com.jshoperxms.controller.mall.backstage.base.SearchModel;
import com.jshoperxms.controller.utils.BaseTools;
import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.enums.GoodsCommentEnums;
import com.jshoperxms.controller.utils.statickey.SerialKeys;
import com.jshoperxms.controller.utils.statickey.StaticKey;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.jshoperxms.controller.utils.statickey.WarnWrods;
import com.jshoperxms.entity.CommunityCircleArticleCommentT;
import com.jshoperxms.entity.TopicArticleCommentT;
import com.jshoperxms.service.CommunityCircleArticleCommentTService;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sdywcd on 16/12/4.
 * Des:
 */
@Controller
@RequestMapping("/mall/communitycirclearticlecomment")
public class CommunityCircleArticleCommentTController extends BaseTController implements IBaseCtrl<CommunityCircleArticleCommentT> {

    @Resource
    private CommunityCircleArticleCommentTService communityCircleArticleCommentTService;


    private Map<String,Object> responsedata=new HashMap<>();

    @RequestMapping(value="/save",method= RequestMethod.POST)
    @Override
    public ResponseEntity save(@RequestBody CommunityCircleArticleCommentT rq) throws ParseException {
        CommunityCircleArticleCommentT bean=new CommunityCircleArticleCommentT();
        bean.setId(this.serial.Serialid(SerialKeys.COMMUNITYCIRCLEARTICLECOMMENT));
        bean.setCai(rq.getCai());
        bean.setCommentcontent(rq.getCommentcontent());
        bean.setCommunitycirclearticleid(rq.getCommunitycirclearticleid());
        bean.setCommunitycirclearticletitle(rq.getCommunitycirclearticletitle());
        bean.setCreatetime(DateTools.getSystemTimeBi());
        bean.setHeadpath(StaticKey.EMPTY);
        bean.setMemberid(StaticKey.EMPTY);
        bean.setReplyid(StaticKey.EMPTY);
        bean.setReplyorcomment(StaticKey.EMPTY);
        bean.setReplyorcommentmemberid(StaticKey.EMPTY);
        bean.setReplyorcommentmembername("系统名称");
        bean.setReviewed(GoodsCommentEnums.CommentReviewed.NO.getState());
        bean.setReviewedtime(BigInteger.valueOf(1L));
        bean.setScore(rq.getScore());
        bean.setStatus(BaseEnums.DataUsingState.USING.getState());
        bean.setStoreid(StaticKey.EMPTY);
        bean.setUpdatetime(DateTools.getSystemTimeBi());
        bean.setVersiont(1);
        bean.setVirtualadd(GoodsCommentEnums.GoodsCommentType.VIRTULADD.getState());
        bean.setZan(rq.getZan());
        this.communityCircleArticleCommentTService.save(bean);
        responsedata.put(TagWords.SUCCESS, true);
        responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
        return Response.success(responsedata);
    }

    //查询具体的一条回复和这条回复的所有回复
    @RequestMapping(value="/find",method= RequestMethod.POST)
    @Override
    public ResponseEntity find(@RequestBody CommunityCircleArticleCommentT rq) {
        if(StringUtils.isBlank(rq.getId())){
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG, WarnWrods.ERROR_PARAMS);
        }else{
            CommunityCircleArticleCommentT bean=this.communityCircleArticleCommentTService.findByPK(CommunityCircleArticleCommentT.class,rq.getId());
            if(bean!=null){
                responsedata.put(TagWords.BEAN,bean);
                Criterion criterion= Restrictions.and(Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState())).add(Restrictions.eq("replyid",bean.getId()));
                List<CommunityCircleArticleCommentT>beanlist=this.communityCircleArticleCommentTService.findByCriteria(CommunityCircleArticleCommentT.class,criterion, Order.desc(TagWords.UPDATETIME));
                responsedata.put(TagWords.BEANLIST,beanlist);
            }
        }
        responsedata.put(TagWords.SUCCESS,true);
        responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
        return Response.success(responsedata);
    }

    @RequestMapping(value="/del",method= RequestMethod.POST)
    @Override
    public ResponseEntity del(@RequestBody CommunityCircleArticleCommentT rq) {
        if(StringUtils.isBlank(rq.getId())){
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG,WarnWrods.ERROR_PARAMS);
        }else{
            String ids[]=StringUtils.split(rq.getId(),TagWords.SPLITDOT);
            for(String s:ids){
                CommunityCircleArticleCommentT bean=this.communityCircleArticleCommentTService.findByPK(CommunityCircleArticleCommentT.class,s);
                if(bean!=null){
                    bean.setStatus(BaseEnums.DataUsingState.DEL.getState());
                    bean.setUpdatetime(DateTools.getSystemTimeBi());
                    this.communityCircleArticleCommentTService.update(bean);
                }
            }
            responsedata.put(TagWords.SUCCESS,true);
            responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
        }
        return Response.success(responsedata);
    }

    //回复评论
    @RequestMapping(value="/reply",method= RequestMethod.POST)
    public ResponseEntity reply(@RequestBody CommunityCircleArticleCommentT rq){
        CommunityCircleArticleCommentT bean=new CommunityCircleArticleCommentT();
        bean.setId(this.serial.Serialid(SerialKeys.COMMUNITYCIRCLEARTICLECOMMENT));
        bean.setCai(0);
        bean.setCommentcontent(rq.getCommentcontent());
        bean.setCreatetime(DateTools.getSystemTimeBi());
        bean.setHeadpath(StaticKey.EMPTY);
        bean.setMemberid(StaticKey.EMPTY);
        bean.setReplyid(rq.getId());
        bean.setReplyorcomment(GoodsCommentEnums.ReplyorComment.REPLY.getState());
        bean.setReplyorcommentmemberid(StaticKey.EMPTY);
        bean.setReplyorcommentmembername("系统名称");
        bean.setReviewed(GoodsCommentEnums.CommentReviewed.NO.getState());
        bean.setReviewedtime(BigInteger.valueOf(1L));
        bean.setScore(rq.getScore());
        bean.setStatus(BaseEnums.DataUsingState.USING.getState());
        bean.setStoreid(StaticKey.EMPTY);
        bean.setCommunitycirclearticleid(rq.getCommunitycirclearticleid());
        bean.setCommunitycirclearticletitle(rq.getCommunitycirclearticletitle());
        bean.setUpdatetime(DateTools.getSystemTimeBi());
        bean.setVersiont(1);
        bean.setVirtualadd(GoodsCommentEnums.GoodsCommentType.VIRTULADD.getState());
        bean.setZan(0);
        this.communityCircleArticleCommentTService.save(bean);
        responsedata.put(TagWords.SUCCESS, true);
        responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
        return Response.success(responsedata);
    }



    @RequestMapping(value="/update",method= RequestMethod.POST)
    @Override
    public ResponseEntity update(@RequestBody CommunityCircleArticleCommentT rq) throws ParseException {
        if(StringUtils.isBlank(rq.getId())){
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG,WarnWrods.ERROR_PARAMS);
        }else{
            CommunityCircleArticleCommentT bean=this.communityCircleArticleCommentTService.findByPK(CommunityCircleArticleCommentT.class,rq.getId());
            if(bean!=null){
                bean.setCommentcontent(rq.getCommentcontent());
                bean.setScore(rq.getScore());
                bean.setStatus(rq.getStatus());
                bean.setVirtualadd(rq.getVirtualadd());
                bean.setZan(rq.getZan());
                bean.setCai(rq.getCai());
                bean.setUpdatetime(DateTools.getSystemTimeBi());
                bean.setVersiont(bean.getVersiont()+1);
                this.communityCircleArticleCommentTService.update(bean);
                responsedata.put(TagWords.SUCCESS,true);
                responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
            }
        }
        return Response.success(responsedata);
    }

    @Override
    public ResponseEntity findAll() {
        return null;
    }



    //查询某个话题文章的评论的回复,比较耗时
    @RequestMapping(value="/findAllReplyByCommentId",method= RequestMethod.POST)
    public ResponseEntity findAllCommentByGoodsId(@RequestBody TopicArticleCommentT rq){
        if(StringUtils.isBlank(rq.getId())){
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG,WarnWrods.ERROR_PARAMS);
        }else{
            //查询评论
            CommunityCircleArticleCommentT bean=this.communityCircleArticleCommentTService.findByPK(CommunityCircleArticleCommentT.class,rq.getId());
            if(bean!=null){
                Criterion criterion=Restrictions.eq("replyid",rq.getId());
                List<CommunityCircleArticleCommentT> goodsCommentTs=this.communityCircleArticleCommentTService.findByCriteria(CommunityCircleArticleCommentT.class,criterion,Order.desc(TagWords.UPDATETIME));
                responsedata.put(TagWords.SUCCESS,true);
                responsedata.put(TagWords.BEAN,bean);
                responsedata.put(TagWords.BEANLIST,goodsCommentTs);
            }
        }
        return Response.success(responsedata);
    }



    //审核评论
    @RequestMapping(value="/reviewCommentById",method= RequestMethod.POST)
    public ResponseEntity reviewCommentById(@RequestBody TopicArticleCommentT rq){
        if(StringUtils.isBlank(rq.getId())){
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG,WarnWrods.ERROR_PARAMS);
        }else{
            CommunityCircleArticleCommentT bean=this.communityCircleArticleCommentTService.findByPK(CommunityCircleArticleCommentT.class,rq.getId());
            if(bean!=null){
                if(StringUtils.equals(rq.getReviewed(),GoodsCommentEnums.CommentReviewed.PASS.getState())){
                    bean.setReviewed(GoodsCommentEnums.CommentReviewed.PASS.getState());
                }
                if(StringUtils.equals(rq.getReviewed(),GoodsCommentEnums.CommentReviewed.REJECT.getState())){
                    bean.setReviewed(GoodsCommentEnums.CommentReviewed.REJECT.getState());
                }
                bean.setUpdatetime(DateTools.getSystemTimeBi());
                bean.setVersiont(bean.getVersiont()+1);
                this.communityCircleArticleCommentTService.update(bean);
                responsedata.put(TagWords.SUCCESS,true);
                responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
            }
        }
        return Response.success(responsedata);

    }


    @RequestMapping(value="/findCommentByCommunityCircleArticleIdByPage",method= RequestMethod.POST)
    public ResponseEntity findCommentByGoodsIdByPage(SearchModel searchModel, @RequestParam("search[value]") String search, @RequestParam("order[0][column]") String ordervalue, @RequestParam("order[0][dir]") String ordertype) {
        findOneCommunityCircleArticleCommentCustomSearch(searchModel,search,ordervalue,ordertype);
        return Response.success(responsedata);
    }

    private void findOneCommunityCircleArticleCommentCustomSearch(SearchModel searchModel, String search, String ordervalue, String ordertype) {
        if(searchModel.getIsCustomSearch()==1){
            int currentPage=searchModel.getCalCurrentPage();
            int lineSize = searchModel.getLength();
            Criterion criterion=Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState());
            if(StringUtils.isNotBlank(searchModel.getObjectid())){
                criterion=Restrictions.and(criterion).add(Restrictions.eq("communitycirclearticleid",searchModel.getObjectid()));
            }
            if(StringUtils.isNotBlank(searchModel.getName())){
                criterion=Restrictions.and(criterion).add(Restrictions.like("commentcontent",searchModel.getName(), MatchMode.ANYWHERE));
            }
            if(StringUtils.isNotBlank(search)){
                criterion=Restrictions.and(criterion).add(Restrictions.like("commentcontent",search,MatchMode.ANYWHERE));
            }
            int records=this.communityCircleArticleCommentTService.count(CommunityCircleArticleCommentT.class,criterion).intValue();
            searchModel.setRecordsTotal(records);
            searchModel.setRecordsFiltered(records);
            responsedata.put(TagWords.RECORDSTOTAL,records);
            responsedata.put(TagWords.RECORDSFILTERED,records);
            List<CommunityCircleArticleCommentT> list = this.communityCircleArticleCommentTService.findByCriteriaByPage(
                    CommunityCircleArticleCommentT.class, criterion, Order.desc(TagWords.UPDATETIME), currentPage,
                    lineSize);
            this.processPageList(list);

        }else{
            if(StringUtils.isNotBlank(search)){
                filterOneCommunityCircleArticleCommentSearch(searchModel,search);
            }else{
                findOneCommunityCircleArticleCommentDefaultAll(searchModel,ordervalue,ordertype);
            }
        }
    }


    private void filterOneCommunityCircleArticleCommentSearch(SearchModel searchModel, String search) {
        int currentPage=searchModel.getCalCurrentPage();
        int lineSize = searchModel.getLength();
        Criterion criterion=Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState());
        if(StringUtils.isNotBlank(searchModel.getGoodsid())){
            criterion=Restrictions.and(criterion).add(Restrictions.eq("communitycirclearticleid",searchModel.getTopicarticleid()));
        }
        criterion=Restrictions.and(criterion).add(Restrictions.like("commentcontent",search,MatchMode.ANYWHERE));
        int records=this.communityCircleArticleCommentTService.count(CommunityCircleArticleCommentT.class,criterion).intValue();
        searchModel.setRecordsTotal(records);
        searchModel.setRecordsFiltered(records);
        responsedata.put(TagWords.RECORDSTOTAL,records);
        responsedata.put(TagWords.RECORDSFILTERED,records);
        List<CommunityCircleArticleCommentT> list = this.communityCircleArticleCommentTService.findByCriteriaByPage(
                CommunityCircleArticleCommentT.class, criterion,Order.desc(TagWords.UPDATETIME), currentPage,
                lineSize);
        this.processPageList(list);
    }


    private void findOneCommunityCircleArticleCommentDefaultAll(SearchModel searchModel, String ordervalue, String ordertype) {
        int currentPage=searchModel.getCalCurrentPage();
        int lineSize = searchModel.getLength();
        Criterion criterion=Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState());
        if(StringUtils.isNotBlank(searchModel.getTopicarticleid())){
            criterion=Restrictions.and(criterion).add(Restrictions.eq("communitycirclearticleid",searchModel.getTopicarticleid()));
        }
        int records=this.communityCircleArticleCommentTService.count(CommunityCircleArticleCommentT.class,criterion).intValue();
        searchModel.setRecordsFiltered(records);
        searchModel.setRecordsTotal(records);
        responsedata.put(TagWords.RECORDSTOTAL,records);
        responsedata.put(TagWords.RECORDSFILTERED,records);
        Order order= BaseTools.orderByParam(TagWords.UPDATETIME,ordervalue,ordertype);
        List<CommunityCircleArticleCommentT> list = this.communityCircleArticleCommentTService.findByCriteriaByPage(
                CommunityCircleArticleCommentT.class, criterion,order, currentPage,
                lineSize);
        this.processPageList(list);
    }



    @RequestMapping(value="/findByPage",method= RequestMethod.POST)
    @Override
    public ResponseEntity findByPage(SearchModel searchModel, @RequestParam("search[value]") String search, @RequestParam("order[0][column]") String ordervalue, @RequestParam("order[0][dir]") String ordertype) {
        findCustomSearch(searchModel,search,ordervalue,ordertype);
        return Response.success(responsedata);
    }

    @Override
    public void findCustomSearch(SearchModel searchModel, String search, String ordervalue, String ordertype) {
        if(searchModel.getIsCustomSearch()==1){
            int currentPage=searchModel.getCalCurrentPage();
            int lineSize = searchModel.getLength();
            Criterion criterion=Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState());
            if(StringUtils.isNotBlank(searchModel.getName())){
                criterion=Restrictions.and(criterion).add(Restrictions.like("commentcontent",searchModel.getName(), MatchMode.ANYWHERE));
            }
            if(StringUtils.isNotBlank(search)){
                criterion=Restrictions.and(criterion).add(Restrictions.like("commentcontent",search,MatchMode.ANYWHERE));
            }
            int records=this.communityCircleArticleCommentTService.count(CommunityCircleArticleCommentT.class,criterion).intValue();
            searchModel.setRecordsTotal(records);
            searchModel.setRecordsFiltered(records);
            responsedata.put(TagWords.RECORDSTOTAL,records);
            responsedata.put(TagWords.RECORDSFILTERED,records);
            List<CommunityCircleArticleCommentT> list = this.communityCircleArticleCommentTService.findByCriteriaByPage(
                    CommunityCircleArticleCommentT.class, criterion, Order.desc(TagWords.UPDATETIME), currentPage,
                    lineSize);
            this.processPageList(list);

        }else{
            if(StringUtils.isNotBlank(search)){
                filterSearch(searchModel,search);
            }else{
                findDefaultAll(searchModel,ordervalue,ordertype);
            }
        }
    }

    @Override
    public void filterSearch(SearchModel searchModel, String search) {
        int currentPage=searchModel.getCalCurrentPage();
        int lineSize = searchModel.getLength();
        Criterion criterion=Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState());
        criterion=Restrictions.and(criterion).add(Restrictions.like("commentcontent",search,MatchMode.ANYWHERE));
        int records=this.communityCircleArticleCommentTService.count(CommunityCircleArticleCommentT.class,criterion).intValue();
        searchModel.setRecordsTotal(records);
        searchModel.setRecordsFiltered(records);
        responsedata.put(TagWords.RECORDSTOTAL,records);
        responsedata.put(TagWords.RECORDSFILTERED,records);
        List<CommunityCircleArticleCommentT> list = this.communityCircleArticleCommentTService.findByCriteriaByPage(
                CommunityCircleArticleCommentT.class, criterion,Order.desc(TagWords.UPDATETIME), currentPage,
                lineSize);
        this.processPageList(list);
    }

    @Override
    public void findDefaultAll(SearchModel searchModel, String ordervalue, String ordertype) {
        int currentPage=searchModel.getCalCurrentPage();
        int lineSize = searchModel.getLength();
        Criterion criterion=Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState());
        int records=this.communityCircleArticleCommentTService.count(CommunityCircleArticleCommentT.class,criterion).intValue();
        searchModel.setRecordsFiltered(records);
        searchModel.setRecordsTotal(records);
        responsedata.put(TagWords.RECORDSTOTAL,records);
        responsedata.put(TagWords.RECORDSFILTERED,records);
        Order order= BaseTools.orderByParam(TagWords.UPDATETIME,ordervalue,ordertype);
        List<CommunityCircleArticleCommentT> list = this.communityCircleArticleCommentTService.findByCriteriaByPage(
                CommunityCircleArticleCommentT.class, criterion,order, currentPage,
                lineSize);
        this.processPageList(list);
    }

    @Override
    public void processPageList(List<CommunityCircleArticleCommentT> list) {
        List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
        for(CommunityCircleArticleCommentT bean:list){
            if(StringUtils.equals(bean.getReplyorcommentmembername(),StaticKey.EMPTY)){
                bean.setReplyorcommentmembername("系统");
            }
            Map<String,Object>cellMap=new HashMap<String, Object>();
            cellMap.put("id", bean.getId());
            cellMap.put("commentcontent",bean.getCommentcontent());
            cellMap.put("communitycirclearticletitle", bean.getCommunitycirclearticletitle());
            cellMap.put("relpyorcommentmembername",bean.getReplyorcommentmembername());
            cellMap.put("virtualadd",GoodsCommentEnums.GoodsCommentType.getName(bean.getVirtualadd()));
            cellMap.put("reviewed", GoodsCommentEnums.CommentReviewed.getName(bean.getReviewed()));
            cellMap.put("communitycirclearticleid", bean.getCommunitycirclearticleid());
            cellMap.put("status", BaseEnums.DataUsingState.getName(bean.getStatus()));
            cellMap.put("updatetime", DateTools.formateLongDateToString(bean.getUpdatetime().longValue()));
            cellMap.put("version", bean.getVersiont());
            data.add(cellMap);
        }
        responsedata.put(TagWords.DATA, data);
    }

    @Override
    public boolean checkDuplicates(String str) {
        return false;
    }
}
